// miniprogram/pages/NX_detailProjectMember/detailProjectMember.js
const db = wx.cloud.database()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        options: {
            projectId: null
        },
        member: [],
        master: [],
        project: null,
        loaded: false,
        self:null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            options
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        const options=this.data.options;
        const projectId = options.projectId;
        this.setData({
            self:getApp().globalData.userInfo
        })
        db.collection('group').doc(projectId).get().then(res => {
            this.setData({ project: res.data });
            wx.setNavigationBarTitle({
                title:res.data.project.projectName
            })
        });
        db.collection('code').where({
            group: projectId
        }).field({
          role: true,
          time: true,
          userId: true
        }).get().then(res => {
          console.log(res)
            if (!res.data || !res.data.length) return;
            const member = [], master = [];
            Promise.all(res.data.map(item =>
                new Promise(resolve => {
                    db.collection('user').doc(item.userId).field({
                        userColor: true,
                        userName: true,
                    }).get().then(res => {
                        const data = { ...res.data, ...item }
                        if (data.role === 'master')
                            master.push({...data,codeTime:Date.parse(item.time)});
                        else
                            member.push({...data,codeTime:Date.parse(item.time)});
                        resolve();
                    })
                })

            )).then(() => {
              
                this.setData({
                    member:member.sort((a,b)=>a.codeTime-b.codeTime),
                    master:master.sort((a,b)=>a.codeTime-b.codeTime)
                })
            })
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
      return {
        title: `邀请加入项目${this.data.project.project.projectName}`,
        path: `/pages/NX_index/index?invite=true&inviter=${this.data.self._id}&projectId=${this.data.project._id}&time=${new Date().getTime()}`,
        imageUrl: '../../images/nexeam_banner.png'
      }
    }
})